# -*- coding:utf-8 -*-
import json
import os


from flask import (
    Flask,
    render_template,
    request)

import meth

file_path = []

app = Flask(__name__)


@app.route(r'/process', methods=['POST'])
def view_process():
    info = meth.get_process_list()
    return json.dumps(info, ensure_ascii=False)


@app.route('/', methods=['GET', 'POST'])
def main_begin():
    dict_disk_user = meth.all_disk_user()
    return render_template('tu.html', udisk=dict_disk_user)


@app.route(r'/ajaxs-folder-list', methods=['GET', 'POST'])
def view_folder_list():
    string = request.values.get('string', None)
    l_file = meth.main_file(string)
    return json.dumps(l_file, ensure_ascii=False)

@app.route(r'/ajaxs-folder-list-con', methods=['GET', 'POST'])
def view_folder_list_con():
    string = request.values.get('string', None)
    l_file = meth.is_file_contain_word(file_path, string)
    return json.dumps(l_file, ensure_ascii=False)

@app.route(r'/ajax-sys-data', methods=['GET', 'POST'])
def view_sys_data():
    l_data = meth.test()
    return json.dumps(l_data, ensure_ascii=False)


# 进程
@app.route('/kill', methods=['POST'])
def kill_process():
    pid = request.form.get('pid') #  传回字符串转为 list
    if not bool(pid):
       return 'no data'
    else:
       ls = [ pid for pid in pid.split(',') if pid.isdigit()]
       ls = set(ls);
       print('value:', ls)
       print('---'*25)
       for pid in ls:
            if pid in meth.data.keys():
            # os.kill(int(pid), 9)
                print(pid,':进程结束成功！')
            # time.sleep(0.5)
            else:
                print(pid,":没有该进程！")
    return 'your content'


# cpu/ram
@app.route('/shuju',methods=['GET', 'POST'])
def view_emptya():
    a=meth.MainRun()
    return json.dumps(float(a))
@app.route('/shuju1',methods=['GET', 'POST'])
def view_emptyb():
    b=meth.MainRun1()
    return json.dumps(float(b))



if __name__ == '__main__':
    RUN_CFG = {
    'host': '127.0.0.1',
    'port': 9000,
    'debug': True
    }
    # file_path = meth.get_all_file_list(r'f:')
    app.run(**RUN_CFG) 

